home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 2 / Macwelt DVD 2.cdr / System / Internet-Utilities / macosx / News Mac 1.1.dmg / Engine.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-04-08  |  11.5 KB  |  452 lines

  1. import java.io.BufferedWriter;
  2. import java.io.FileInputStream;
  3. import java.io.FileWriter;
  4. import java.io.Writer;
  5. import java.util.Properties;
  6. import java.util.StringTokenizer;
  7. import java.util.Vector;
  8.  
  9. public class Engine {
  10.    protected ChannelList database = new ChannelList();
  11.    protected Config theConfig = new Config();
  12.    protected Vector keywords = new Vector(100, 10);
  13.  
  14.    public Engine(Config systemConfig) {
  15.       this.theConfig = systemConfig;
  16.    }
  17.  
  18.    protected void updateHeadlines() throws Exception {
  19.       for(int i = 0; i < this.database.size(); ++i) {
  20.          Channel tempChannel = this.database.getChannel(i);
  21.          Parser theParser = new Parser(tempChannel);
  22.  
  23.          try {
  24.             tempChannel.setHeadlines(theParser.findHeadlines());
  25.             tempChannel.setDownloaded(true);
  26.             this.database.setChannel(i, tempChannel);
  27.          } catch (Exception var6) {
  28.             String[] temp = new String[]{"NewsMac Error", ((Throwable)var6).toString().substring(21, ((Throwable)var6).toString().length())};
  29.             tempChannel.setHeadlines(temp);
  30.             tempChannel.setDownloaded(false);
  31.             this.database.setChannel(i, tempChannel);
  32.          }
  33.       }
  34.  
  35.    }
  36.  
  37.    protected void updateHeadlines(Channel theChannel) throws Exception {
  38.       Parser theParser = new Parser(theChannel);
  39.  
  40.       try {
  41.          theChannel.setHeadlines(theParser.findHeadlines());
  42.          this.database.setChannel(this.getChannelIndex(theChannel), theChannel);
  43.          theChannel.setDownloaded(true);
  44.       } catch (Exception var5) {
  45.          String[] temp = new String[]{"NewsMac Error", ((Throwable)var5).toString().substring(21, ((Throwable)var5).toString().length())};
  46.          theChannel.setHeadlines(temp);
  47.          theChannel.setDownloaded(false);
  48.       }
  49.  
  50.    }
  51.  
  52.    protected String[] getChannelFiles(String path) throws Exception {
  53.       try {
  54.          String[] temp = IOUtil.dirListing(path);
  55.          return temp;
  56.       } catch (Exception var4) {
  57.          System.out.println(var4);
  58.          throw new Exception("Error locating channel files!");
  59.       }
  60.    }
  61.  
  62.    private int getChannelIndex(Channel theChannel) throws Exception {
  63.       for(int i = 0; i < this.database.size(); ++i) {
  64.          if (this.database.getChannel(i).getName().equals(theChannel.getName())) {
  65.             return i;
  66.          }
  67.       }
  68.  
  69.       throw new Exception("Channel not in database!");
  70.    }
  71.  
  72.    protected ChannelList getChannels() {
  73.       return this.database;
  74.    }
  75.  
  76.    protected ChannelList getCategory(int category) {
  77.       ChannelList tempList = new ChannelList();
  78.  
  79.       for(int i = 0; i < this.database.size(); ++i) {
  80.          Channel tempChannel = this.database.getChannel(i);
  81.          if (tempChannel.getCategory() == category) {
  82.             tempList.addChannel(tempChannel);
  83.          }
  84.       }
  85.  
  86.       return tempList;
  87.    }
  88.  
  89.    protected String getKeywords() {
  90.       String temp = "";
  91.  
  92.       for(int i = 0; i < this.keywords.size(); ++i) {
  93.          if (i + 1 == this.keywords.size()) {
  94.             temp = temp + (String)this.keywords.elementAt(i);
  95.          } else {
  96.             temp = temp + (String)this.keywords.elementAt(i) + ", ";
  97.          }
  98.       }
  99.  
  100.       return temp;
  101.    }
  102.  
  103.    protected ChannelList getFavorites() {
  104.       ChannelList tempList = new ChannelList();
  105.  
  106.       for(int i = 0; i < this.database.size(); ++i) {
  107.          Channel tempChannel = this.database.getChannel(i);
  108.          if (tempChannel.isFavorite()) {
  109.             tempList.addChannel(tempChannel);
  110.          }
  111.       }
  112.  
  113.       return tempList;
  114.    }
  115.  
  116.    protected boolean loadKeywords() {
  117.       this.keywords = new Vector(100, 10);
  118.  
  119.       Vector temp;
  120.       try {
  121.          temp = IOUtil.openTextFile(this.theConfig.getKeywordFile());
  122.       } catch (Exception var6) {
  123.          System.out.println("Engine: Could not load keywords file, using defaults.\n\t" + var6);
  124.          this.keywords.add("Mac");
  125.          this.keywords.add("Apple");
  126.          this.keywords.add("NewsMac");
  127.          this.keywords.add("OS X");
  128.          return false;
  129.       }
  130.  
  131.       String tempString = "";
  132.  
  133.       for(int i = 0; i < temp.size(); ++i) {
  134.          tempString = tempString + (String)temp.elementAt(0);
  135.       }
  136.  
  137.       StringTokenizer st = new StringTokenizer(tempString, ",");
  138.       int n = 0;
  139.  
  140.       while(st.hasMoreTokens()) {
  141.          this.keywords.add(st.nextToken().trim());
  142.       }
  143.  
  144.       return true;
  145.    }
  146.  
  147.    protected Channel openChannel(String fileName) throws Exception {
  148.       Channel temp = new Channel();
  149.  
  150.       try {
  151.          Properties props = new Properties();
  152.          props.load(new FileInputStream(fileName));
  153.          temp.setName(props.getProperty("name"));
  154.          temp.setURL(props.getProperty("url"));
  155.          temp.setSource(props.getProperty("source"));
  156.          temp.setDescription(props.getProperty("description"));
  157.          temp.setHeadlineCount(new Integer(props.getProperty("capture")));
  158.          temp.setCategory(new Integer(props.getProperty("category")));
  159.          temp.setVersion(new Integer(props.getProperty("version")));
  160.          if (props.getProperty("cache").equalsIgnoreCase("yes")) {
  161.             temp.setCached(true);
  162.          } else {
  163.             temp.setCached(false);
  164.          }
  165.  
  166.          temp.setStartMarker(props.getProperty("start_marker"));
  167.          temp.setEndMarker(props.getProperty("end_marker"));
  168.          temp.setOpeningMarker(props.getProperty("headline_start"));
  169.          temp.setClosingMarker(props.getProperty("headline_end"));
  170.          return temp;
  171.       } catch (Exception var4) {
  172.          throw new Exception("Error reading channel " + fileName + "\n");
  173.       }
  174.    }
  175.  
  176.    protected boolean loadAllChannels(boolean system) {
  177.       String[] dirListing = new String[]{""};
  178.       boolean noError = true;
  179.  
  180.       try {
  181.          if (system) {
  182.             this.database = new ChannelList();
  183.             Vector channelDatabase = IOUtil.openTextFile("newsmac.app/contents/resources/channel_database.data");
  184.  
  185.             for(int i = 0; i < channelDatabase.size(); ++i) {
  186.                Channel temp = new Channel();
  187.                StringTokenizer st = new StringTokenizer((String)channelDatabase.elementAt(i), "~~");
  188.  
  189.                while(st.hasMoreTokens()) {
  190.                   try {
  191.                      if (i <= 0) {
  192.                         this.database.setVersion(new Integer(st.nextToken()));
  193.                      } else {
  194.                         temp.setName(st.nextToken());
  195.                         temp.setURL(st.nextToken());
  196.                         temp.setSource(st.nextToken());
  197.                         temp.setDescription(st.nextToken());
  198.                         temp.setStartMarker(st.nextToken());
  199.                         temp.setEndMarker(st.nextToken());
  200.                         temp.setOpeningMarker(st.nextToken());
  201.                         temp.setClosingMarker(st.nextToken());
  202.                         temp.setVersion(new Integer(st.nextToken()));
  203.                         temp.setCategory(new Integer(st.nextToken()));
  204.                         temp.setHeadlineCount(new Integer(st.nextToken()));
  205.                         temp.setMaxHeadlineCount(new Integer(st.nextToken()));
  206.                         if (temp.getStartMarker().equals(" ")) {
  207.                            temp.setStartMarker("");
  208.                         }
  209.  
  210.                         if (temp.getEndMarker().equals(" ")) {
  211.                            temp.setEndMarker("");
  212.                         }
  213.  
  214.                         int channelCounter = 0;
  215.                         if (this.database.size() <= 5) {
  216.                            this.database.addChannel(temp);
  217.                         } else {
  218.                            for(int z = 0; z < this.database.size(); ++z) {
  219.                               if (this.database.getChannel(z).getName().equals(temp.getName())) {
  220.                                  ++channelCounter;
  221.                               }
  222.                            }
  223.  
  224.                            if (channelCounter < 2) {
  225.                               this.database.addChannel(temp);
  226.                            }
  227.                         }
  228.                      }
  229.                   } catch (Exception var11) {
  230.                      System.out.println("Engine: Error loading channel file from database\n\t" + var11);
  231.                   }
  232.                }
  233.             }
  234.          } else if (IOUtil.fileExists(this.theConfig.getHome() + "/library/channels/")) {
  235.             dirListing = this.getChannelFiles(this.theConfig.getHome() + "/library/channels/");
  236.  
  237.             for(int i = 0; i < dirListing.length; ++i) {
  238.                try {
  239.                   if (dirListing[i].endsWith(".channel")) {
  240.                      this.database.addChannel(this.openChannel(this.theConfig.getHome() + "/library/channels/" + dirListing[i]));
  241.                   }
  242.                } catch (Exception var10) {
  243.                   System.out.println("Engine: Could not load channel " + dirListing[i] + "!\n\t" + var10);
  244.                   noError = false;
  245.                }
  246.             }
  247.          }
  248.  
  249.          if (dirListing.length == 0) {
  250.             throw new Exception("No channels were found!");
  251.          } else {
  252.             return noError;
  253.          }
  254.       } catch (Exception var12) {
  255.          System.out.println("Engine: Could not load channels!\n\t" + var12);
  256.          return false;
  257.       }
  258.    }
  259.  
  260.    protected void saveChannelSettings() throws Exception {
  261.       try {
  262.          BufferedWriter out = new BufferedWriter(new FileWriter(this.theConfig.getHome() + "/library/preferences/NewsMac Channel Settings", false));
  263.  
  264.          for(int i = 0; i < this.database.size(); ++i) {
  265.             Channel tempChannel = this.database.getChannel(i);
  266.             String temp = tempChannel.getName() + "~~" + tempChannel.isFavorite() + "~~" + tempChannel.isOn() + "~~" + tempChannel.isEnabled() + "\n";
  267.             ((Writer)out).write(temp);
  268.          }
  269.  
  270.          out.close();
  271.       } catch (Exception var5) {
  272.          throw new Exception("Unable to save channel settings file!");
  273.       }
  274.    }
  275.  
  276.    protected void applyChannelSettings() throws Exception {
  277.       try {
  278.          if (IOUtil.fileExists(this.theConfig.getHome() + "/library/preferences/NewsMac Channel Settings")) {
  279.             Vector channelStates = IOUtil.openTextFile(this.theConfig.getHome() + "/library/preferences/NewsMac Channel Settings");
  280.             String channelName = "";
  281.             String channelFavorite = "";
  282.             String channelOn = "";
  283.             String channelEnabled = "";
  284.  
  285.             for(int i = 0; i < channelStates.size(); ++i) {
  286.                StringTokenizer st = new StringTokenizer((String)channelStates.elementAt(i), "~~");
  287.  
  288.                try {
  289.                   channelName = st.nextToken().trim();
  290.                   channelFavorite = st.nextToken().trim();
  291.                   channelOn = st.nextToken().trim();
  292.                   channelEnabled = st.nextToken().trim();
  293.                } catch (Exception var10) {
  294.                   System.out.println("Error processing settigns for line " + i);
  295.                }
  296.  
  297.                for(int j = 0; j < this.database.size(); ++j) {
  298.                   Channel temp = this.database.getChannel(j);
  299.                   if (temp.getName().equals(channelName)) {
  300.                      temp.setFavorite(false);
  301.                      temp.setEnabled(false);
  302.                      temp.setOn(false);
  303.                      if (channelFavorite.equals("true")) {
  304.                         temp.setFavorite(true);
  305.                      }
  306.  
  307.                      if (channelOn.equals("true")) {
  308.                         temp.setOn(true);
  309.                      }
  310.  
  311.                      if (channelEnabled.equals("true") & temp.isFavorite() || channelEnabled.equals("true") & this.theConfig.isSaveAll()) {
  312.                         temp.setEnabled(true);
  313.                      }
  314.  
  315.                      this.database.setChannel(j, temp);
  316.                   }
  317.                }
  318.             }
  319.          }
  320.  
  321.       } catch (Exception var11) {
  322.          System.out.println(var11);
  323.          throw new Exception("Channel settings file could not be read!\n" + var11);
  324.       }
  325.    }
  326.  
  327.    protected Vector recommendedList() throws Exception {
  328.       Vector tempList = new Vector(100, 50);
  329.       if (this.keywords.size() > 0) {
  330.          for(int i = 0; i < this.database.size(); ++i) {
  331.             Channel tempChannel = this.database.getChannel(i);
  332.             String[] headlines = tempChannel.getHeadlines();
  333.  
  334.             for(int j = 0; j < headlines.length; ++j) {
  335.                int hits = 0;
  336.  
  337.                for(int k = 0; k < this.keywords.size(); ++k) {
  338.                   try {
  339.                      if (headlines[j] != null && ((String)this.keywords.elementAt(k)).length() <= headlines[j].length() && StringUtil.contains(headlines[j], (String)this.keywords.elementAt(k))) {
  340.                         ++hits;
  341.                      }
  342.                   } catch (Exception var9) {
  343.                      throw new Exception("Error while creating personalised news.");
  344.                   }
  345.                }
  346.  
  347.                if (hits > 0) {
  348.                   tempList.add(new Headline(tempChannel.getName(), tempChannel.getURL(), headlines[j], hits, tempChannel.getCategory()));
  349.                }
  350.             }
  351.          }
  352.  
  353.          for(int c = 0; c < tempList.size(); ++c) {
  354.             for(int i = 0; i < tempList.size() - 1; ++i) {
  355.                if (((Headline)tempList.elementAt(i)).getKeywordMatch() < ((Headline)tempList.elementAt(i + 1)).getKeywordMatch()) {
  356.                   Headline tempHeadline = (Headline)tempList.elementAt(i);
  357.                   tempList.setElementAt(tempList.elementAt(i + 1), i);
  358.                   tempList.setElementAt(tempHeadline, i + 1);
  359.                }
  360.             }
  361.          }
  362.       }
  363.  
  364.       return tempList;
  365.    }
  366.  
  367.    protected Vector searchHeadlines(String searchString) throws Exception {
  368.       Vector tempList = new Vector(100, 50);
  369.       Vector searchWords = new Vector(10, 5);
  370.       StringTokenizer st = new StringTokenizer(searchString, " ");
  371.  
  372.       while(st.hasMoreTokens()) {
  373.          searchWords.add(st.nextToken().trim());
  374.       }
  375.  
  376.       if (searchWords.size() > 0) {
  377.          for(int i = 0; i < this.database.size(); ++i) {
  378.             Channel tempChannel = this.database.getChannel(i);
  379.             String[] headlines = tempChannel.getHeadlines();
  380.  
  381.             for(int j = 0; j < headlines.length; ++j) {
  382.                int hits = 0;
  383.  
  384.                for(int k = 0; k < searchWords.size(); ++k) {
  385.                   try {
  386.                      if (headlines[j] != null && ((String)searchWords.elementAt(k)).length() <= headlines[j].length() && StringUtil.contains(headlines[j], (String)searchWords.elementAt(k))) {
  387.                         ++hits;
  388.                      }
  389.                   } catch (Exception var12) {
  390.                      throw new Exception("Error while searching headlines!");
  391.                   }
  392.                }
  393.  
  394.                if (hits > 0) {
  395.                   tempList.add(new Headline(tempChannel.getName(), tempChannel.getURL(), headlines[j], hits, tempChannel.getCategory()));
  396.                }
  397.             }
  398.          }
  399.  
  400.          for(int c = 0; c < tempList.size(); ++c) {
  401.             for(int i = 0; i < tempList.size() - 1; ++i) {
  402.                if (((Headline)tempList.elementAt(i)).getKeywordMatch() < ((Headline)tempList.elementAt(i + 1)).getKeywordMatch()) {
  403.                   Headline tempHeadline = (Headline)tempList.elementAt(i);
  404.                   tempList.setElementAt(tempList.elementAt(i + 1), i);
  405.                   tempList.setElementAt(tempHeadline, i + 1);
  406.                }
  407.             }
  408.          }
  409.       }
  410.  
  411.       return tempList;
  412.    }
  413.  
  414.    protected void startupActions(NewsMac parent) {
  415.       if (this.theConfig.isAutoDownload()) {
  416.          if (this.theConfig.isDownloadFavorites()) {
  417.             this.downloadChannels(parent, this.getFavorites(), this.theConfig.isMyNewsSwitch());
  418.          } else if (this.theConfig.isDownloadAdd()) {
  419.             this.downloadChannels(parent, this.database, this.theConfig.isMyNewsSwitch());
  420.          }
  421.       }
  422.  
  423.    }
  424.  
  425.    protected void downloadChannels(NewsMac parent, ChannelList channelsToDownload, boolean autoSwitch) {
  426.       try {
  427.          if (!this.theConfig.isMyNewsSwitch()) {
  428.             autoSwitch = false;
  429.          }
  430.  
  431.          if (!this.theConfig.isBroadband()) {
  432.             Thread queue = new QueuedDownload(parent, channelsToDownload, autoSwitch);
  433.             queue.start();
  434.          } else {
  435.             for(int i = 0; i < channelsToDownload.size(); ++i) {
  436.                Channel tempChannel = channelsToDownload.getChannel(i);
  437.                tempChannel.setDownloaded(false);
  438.                String[] temp = new String[]{"Updating..."};
  439.                tempChannel.setHeadlines(temp);
  440.                Thread theDownloader = new Downloader(tempChannel, parent, autoSwitch);
  441.                theDownloader.start();
  442.             }
  443.  
  444.             parent.updatePanel(parent.currentPanel);
  445.          }
  446.       } catch (Exception var8) {
  447.          System.out.println("Engine: " + var8);
  448.       }
  449.  
  450.    }
  451. }
  452.